/*

Mikhail Breslav and Chris Kwan
{breslav, ckwan}@cs.bu.edu
http://cs-people.bu.edu/ckwan/cs511/

CS 511 Spring 2011 Project
Client-side Parsing and Verification

Parser for Propositional Logic

Last update: 04/24/2011

*/


/*
 Initialize text areas and class variables
*/
var message = "Enter an expression, then press Shift-Enter to evaluate it.";        

var inputBox = $("inputBox");
var outputBox = $("outputBox");


var expressionString = ""; //the expression string being parsed, most methods reference this


function clearBoxes()
{
outputBox.value = "";
}


/*
 Call update method if user presses Shift+Enter.
*/
function keypress(key)
{
    var code = key.charCode;
    if( code == 0 || code == undefined)
        code = key.keyCode;
        
    if(code == 13 && key.shiftKey)
        update();
}    


/*
 Pass the expression from the input box to the parser.
*/
function update()
{
    var input = inputBox.value;
    
    clearBoxes();
    alert(input.length);
    
    var input_atoms = new Array();
    var temp_string = "";
    var count_atoms = 0;
    var count_chars = 0;

    
   while(count_chars < input.length)
    {
		while(input.charAt(count_chars) != '' && input.charAt(count_chars) != '\n')
		{
				temp_string = temp_string + input.charAt(count_chars);
				count_chars++;
		}
		count_chars++;
		
		input_atoms[count_atoms] = temp_string;
		count_atoms++;
		temp_string = "";
	}
	
	
	
	//for each propositional atom we want to 
	// 1.) check if atom is in the table
	// 2.) if yes, return its truth value
	// 3.) if no, add the atom to the table, give it a truth value, return its truth value
	
	var table = new hash_atoms('ab',true);
		
	for(i=0; i<input_atoms.length;i++)
	{
	var inTable = table.hasItem(input_atoms[i]); 
	
	if(inTable)
	{
		outputBox.value = outputBox.value + input_atoms[i] + ' in table ' + '\n';
	}
	else
	{
		table.setItem(input_atoms[i],true);
		outputBox.value = outputBox.value + input_atoms[i] + ' added to table ' + '\n';
	}
	
	
    //outputBox.value = outputBox.value + input_atoms[i] + '\n';
    
    }
    
   // 	table.setItem(input_atoms[i-1],false);
//     outputBox.value = outputBox.value + input_atoms[i-1] + ' changed to' + table.getItem(input_atoms[i-1]) +  '\n';
    
}


function hash_atoms()
{
	this.length = 0;
	this.items = new Array();
	for (var i = 0; i < arguments.length; i += 2) {
		if (typeof(arguments[i + 1]) != 'undefined') {
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}
	
	this.removeItem = function(in_key)
	{
		var tmp_previous;
		if (typeof(this.items[in_key]) != 'undefined') {
			this.length--;
			var tmp_previous = this.items[in_key];
			delete this.items[in_key];
		}
	   
		return tmp_previous;
	}

	this.getItem = function(in_key) {
		return this.items[in_key];
	}

	this.setItem = function(in_key, in_value)
	{
		var tmp_previous;
		if (typeof(in_value) != 'undefined') {
			if (typeof(this.items[in_key]) == 'undefined') {
				this.length++;
			}
			else {
				tmp_previous = this.items[in_key];
			}

			this.items[in_key] = in_value;
		}
	   
		return tmp_previous;
	}

	this.hasItem = function(in_key)
	{
		return typeof(this.items[in_key]) != 'undefined';
	}

	this.clear = function()
	{
		for (var i in this.items) {
			delete this.items[i];
		}

		this.length = 0;
	}
	
	
	
	
}





/*
 Shortcut to get a document element by id.
*/
function $(id)
{
    return document.getElementById(id);
}



